package com.bdqn.service;

import com.bdqn.base.mybatis.ServiceImpl;
import com.bdqn.mapper.ShopUserMapper;
import com.bdqn.pojo.ShopUser;
import com.bdqn.pojo.ShopUserCountRateVO;
import com.bdqn.pojo.ShopUserVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户
 *
 * @author lilibo
 * @date 2021/10/09
 * @date 2021/10/05
 */
@Service
public class ShopUserServiceImpl extends ServiceImpl<ShopUserMapper, ShopUser> implements ShopUserService {

    /**
     * 用户数统计接口
     *
     * @return
     */
    @Override
    public ShopUserCountRateVO getCountRate() {
        ShopUserCountRateVO shopUserCountRateVO = new ShopUserCountRateVO();
        // 获得用户表总数
        int userCount = baseMapper.selectCount(null);
        shopUserCountRateVO.setUserCount(userCount);
        // 今天新增用户量
        int todayAdd = baseMapper.getTodayAdd();
        // 昨天新增用户量
        int yesterdayAdd = baseMapper.getYesterdayAdd();
        // 前七天新增用户量
        int weekAdd = baseMapper.getWeekAdd();
        // 前七天新增用户平均值
        double weekAddAvg = weekAdd / 7.0;

        // 计算比昨天
        double yesterdayRate = 0;
        if (yesterdayAdd != 0) {
            yesterdayRate = (todayAdd - yesterdayAdd) / yesterdayAdd;
        }
        shopUserCountRateVO.setYesterdayRate(yesterdayRate);
        // 计算比七日
        double weekRate = 0;
        if (weekAddAvg != 0) {
            weekRate = (todayAdd - weekAddAvg) / weekAddAvg;
        }
        shopUserCountRateVO.setWeekRate(weekRate);
        return shopUserCountRateVO;
    }


    /**
     * 获取用户和账户的数据
     * @return
     */
    @Override
    public List<ShopUserVO> getAll() {
        return baseMapper.getUserList();
    }


}
